<html>
    <head>
        <script src="scripts/mootools-1.2.5-core-yc.js"></script>
        <script src="scripts/greasemonkey_layer.js"></script>
        <script>
GM_log("starting background page");
var count = 0;

GM_log("creating func");
var func = function() {
	++count;
	GM_log("DEBUG: count=" + count);
	/*chrome.tabs.query({
	//	url: "www.flickr.com" -> does not work!?
	    }, 
	    function (tabs) {
	    	GM_log("found " + tabs.length + " tabs");
			//flickrTabs = tabs.filter(function (tab) { tab.url.match('www.flickr.com'); });
			//GM_log("tabs: " + tabs);
		//GM_log("of which " + flickrTabs.length + " for Flickr");
		return;
			$each(flickrTabs, function (tab) {
				var tabId = tab.id;
				GM_log("sending count to tabId " + tabId);
				chrome.tabs.sendRequest(tabId, { id: 'NFPN.counter', event: 'update-count', data: count });
			});
	    }
	);*/
	//port.postMessage({callbackEvent: msg.callbackEvent, callbackId: msg.callbackId, stat: 'success', count: count});
}

GM_log("calling func");
func();

GM_log("setting periodical");
var periodicalId = func.periodical(20000);

GM_log("adding onConnect listener");

chrome.extension.onConnect.addListener(function (port) {
    console.assert(port.name == 'flickrnotifications');
    port.onMessage.addListener(function (msg) {
        if (msg.name == 'getVersion') {
            new Request({
                url: "manifest.json",
                onSuccess: function (responseText, responseXML) {
                    var data;
                    try {
                       data = JSON.parse(responseText);
                    } catch (e) {
                        port.postMessage({callbackEvent: msg.callbackEvent, callbackId: msg.callbackId, stat: 'error', statusText: e});
                        return;
                    }
                    port.postMessage({callbackEvent: msg.callbackEvent, callbackId: msg.callbackId, stat: 'success', version: data.version});
                },
                onFailure: function (response) {
                    port.postMessage({callbackEvent: msg.callbackEvent, callbackId: msg.callbackId, stat: 'error', statusText: response.statusText});
                }
            }).get();
	}
    });
});
        </script>
    </head>
</html>
